import ajaxMainTableData from './ajaxMainTableData';
import updateButtonStatus from "./updateButtonStatus";

export default function getMainTableData(isAfterBatch) {
    const {
        searchModalValue,
        currentSelectTreeNode,
        orgVal,
        queryOid,
        isInJob,
        pageInfo,
        selectedPsn,
        selectedPsnPk
    } = this.state;

    let postData = {
        ...searchModalValue,
        tree_node_id: currentSelectTreeNode,
        node_type: "ORG_NODE",
        pk_org: orgVal.refpk,
        func_type: this.props.func_type,
        isInJob,
        pageInfo: pageInfo
    };

    return ajaxMainTableData(postData)
        .then((res) => {
            if (res.success) {
                if (!res.data) {
                    res.data = {
                        'info': {
                            rows: []
                        }
                    };
                }
                res.data['info'].rows.map((item) => {
                    if (item.values['showorder'].value === '9999999') {
                        item.values['showorder'].value = '';
                    }
                });
                this.setState({
                    searchModalVisible: false,
                    hasSearched: true,
                    pageInfo: res.data['info'].pageInfo || {
                        ...pageInfo,
                        pageIndex: 1,
                        total: 0,
                        totalPage: 1
                    },
                }, () => {
                    updateButtonStatus.call(this);
                });
                this.props.table.setAllTableData('info', {
                    rows: res.data['info'].rows
                });
                if (res.data['info'].rows.length > 0) {
                    let selectedIndex = 0;
                    if (isAfterBatch) {
                        selectedIndex = res.data['info'].rows.findIndex(item => item.values.pk_psndoc.value === selectedPsnPk);
                        if (selectedIndex === -1) {
                            selectedIndex = 0;
                            this.setState({
                                selectedPsn: res.data['info'].rows[0].values.pk_psnjob.value,
                                selectedPsnPk: res.data['info'].rows[0].values.pk_psndoc.value,
                                focusedRow: 0
                            });
                        } else {
                            if (res.data['info'].rows[selectedIndex].values.pk_psnjob.value === selectedPsn) {
                                this.subTableNode.getSubTableData(selectedPsn);
                            } else {
                                this.setState({
                                    selectedPsn: res.data['info'].rows[selectedIndex].values.pk_psnjob.value,
                                    selectedPsnPk: res.data['info'].rows[selectedIndex].values.pk_psndoc.value,
                                    focusedRow: selectedIndex
                                });
                            }
                        }
                    } else {
                        this.setState({
                            selectedPsn: res.data['info'].rows[0].values.pk_psnjob.value,
                            selectedPsnPk: res.data['info'].rows[0].values.pk_psndoc.value,
                            focusedRow: selectedIndex
                        });
                    }
                    this.props.table.setClickRowIndex('info', {
                        record: res.data['info'].rows[selectedIndex].values,
                        index: selectedIndex
                    });
                    this.props.table.focusRowByIndex('info', selectedIndex);
                } else {
                    this.setState({
                        selectedPsn: '',
                        selectedPsnPk: '',
                        focusedRow: ''
                    });
                }
            }
        });
}
